<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>title</title>
	<link rel="stylesheet" href="../../resource/weuimin.css"/>
	<link rel="stylesheet" href="../../resource/example.css"/>

	<script src="../../template/js/jquery.js"></script>
	<script src="../../template/js/com/template.js"></script>
	<script src="../../resource/my.js"></script>
	<script src="../../script/api.js"></script>
    <script type="text/javascript" src="../../script/constants.js"></script>
      <link rel="stylesheet" href="../../resource/jinsuistyle.css" />
    <script src="../resource/md5.js"></script>
    <script type="text/javascript" src="../../script/tmd5.js"></script>
    <script type="text/javascript" src="../../script/aes.js"></script>
	<script type="text/javascript" charset="UTF-8" src="../../script/naes.js"></script>
	<script type="text/javascript" src="../../script/modeecb.js"></script>
    <style>
    	body {
	    }
    	.bgColor {
    		
    	}
		.btn{
		 margin-bottom: 20px
		}
		.btnn{
		 width:90%;
		 font-size:20px;
		 margin:0 auto;
		 height:42px;
		 border-radius:5px;
         line-height: 42px;
		}
    </style>
</head>
<body style="background: #fcfcfc"> 
<div>
	<div style="">
		<div style="width:100%;clear:both">
		<div style="font-size: 20px;padding-top:10px;text-align: center">请选择支付方式</div>
		
		<!-- <input type="button" onclick="closeInfo()" style="font-size:xx-large;float:right;margin-right:5%;" value="&times"/> -->
		</div>
	</div>
	<div style="clear:both;bottom:0;position:absolute;width:100%;text-align:center">
		<div class="btn"><div id="btnn" class="btnn" style="background: #fff;" onclick="unionPay();">银联支付</div></div>
		<div class="btn"><div id="btnn" class="btnn" style="background: #fff;" onclick="payOrder();">支付宝</div></div>
		<div class="btn"><div id="btnn" class="btnn" onclick="closeInfo();" style="background: #ff5151;color:#fff">取消</div></div>
		</div>
		</div>
		
		 <div id="loadingToast" style="display:none;">
		        <div class="weui-mask_transparent"></div>
		        <div class="weui-toast">
		            <i class="weui-loading weui-icon_toast"></i>
		            <p class="weui-toast__content">数据提交中...</p>
		        </div>
			</div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../resource/jquery1.js"></script>
<script type="text/javascript">
    utils = {
	    setParam : function (name,value){
	        localStorage.setItem(name,value)
	    },
	    getParam : function(name){
	        return localStorage.getItem(name)
	    }
	}
    var orderid = "";
    var seli = 0;
    var zje = "";
    var bsh = "";
	var bdzdh = "";
	var bkhh = "";
	var bfplx = "";
	var bmc = "";
	var amc = "";
	var adh = "";
	var adz = "";
	var dzObj = null;
	var num = 0;
	var allPrice = 0;
	var allspmc = "";
	var isCard = 0;
	apiready = function(){
	  num = api.pageParam.num;
	  dzObj = api.pageParam.dzObj;
      orderid = api.pageParam.orderid;
      seli = api.pageParam.seli;
      zje = api.pageParam.zje;
      bsh = api.pageParam.bsh;
	  bdzdh = api.pageParam.bdzdh;
	  bkhh = api.pageParam.bkhh;
	  bfplx = api.pageParam.bfplx;
	  bmc = api.pageParam.bmc;
	  amc = api.pageParam.amc;
	  adh = api.pageParam.adh;
	  adz = api.pageParam.adz;
	  allPrice = api.pageParam.zje;
	  allspmc = api.pageParam.allspmc;
	  adz = adz.replace(/,/g,'_');
	};
	function closeInfo(){
		if(api.systemType == "android"){
		 $api.setStorage("frame","");
		 }
		var jsfun = 'funcGoto();';
		api.execScript({
		    script: jsfun
		});
		api.closeFrame({
	    });
	    
	}
	
	function closeInfo1(){
		if(api.systemType == "android"){
		 $api.setStorage("frame","");
		 }
		var jsfun = 'funcGoto1();';
		api.execScript({
		    script: jsfun
		});
		api.closeFrame({
	    });
	    
	}
	

   	function payOrder(){
   	    var jsfun = 'show();';
		api.execScript({
		    script: jsfun
		});
  		var _openid = $api.getStorage('openid');
  		var dl_phone = $api.getStorage("dl_phone");
  		var ip = "0.0.0.0";
		var ipAddr = api.require('ipAddress');
		var s = api.connectionType;
	    s = s.toLowerCase();
	    ipAddr.getIp(
		function(ret, err) {
			if (ret.status) {
				ip = ret.ip;
			}
		});
		//console.info(dl_phone+","+orderid+","+amc+","+adh+","+adz+","+bmc+","+bsh+","+bdzdh+","+bkhh+","+bfplx+","+allPrice);
      var content = {"dlPhone":dl_phone,"ip":ip,"deviceID":_openid,"openid":_openid,"orderid":orderid,"amc":amc,"adh":adh,"adz":adz,"bmc":bmc,"bsh":bsh,
      "bdzdh":bdzdh,"bkhh":bkhh,"bfplx":bfplx,"je":allPrice,"phone":dl_phone};
      var callback = function(data, err){
            console.log(JSON.stringify(data));
            if(data.msg == "Y"){
	    		pay(data.payid,allPrice);
	    	}else if(data.msg == "N"){
	    		 var jsfun = 'hide();';
					api.execScript({
					    script: jsfun
					});
	    		alert("库存不足！");
	    	}else{
	    		 var jsfun = 'hide();';
					api.execScript({
					    script: jsfun
					});
	    		alert("支付失败，稍候再试！");
	    	}
      }
      encApiAjax(JSON.stringify(content),url.scddh,callback);
   	}
   	
   	function pay(payid,zje){
   	  var _body = orderid + "," + allspmc; //商品描述
   	  var _amount = allPrice;  //总价
   	  var _tradeNO = orderid;  //订单号
   	  var je = zje + "";
	  if(je.indexOf(".") == -1){
	  		je = je + ".00";
	  }else if(je.split(".")[1].length == 1){
	  		je = je + "0";
	  }

   	 var aliPay = api.require('aliPay');
		aliPay.pay({
		  	subject: '职场生活商品交易',
		 	body: _tradeNO,
			amount: je,
			tradeNO: payid
		}, function(ret, err) {
		 var jsfun = 'hide();';
			api.execScript({
			    script: jsfun
			});
			if(ret.code == 9000){
				api.alert({
			        title: '支付结果',
 			       msg: '支付成功！',
 			       buttons: ['确定']
				});
				localStorage.setItem("ShoppingCart", "");
				closeInfo1();
			}else if(ret.code == 6001){
				api.alert({
   			     title: '支付结果',
    			    msg: '取消支付。',
    			    buttons: ['确定']
  			  });
  			    closeInfo1();
			}else{
				api.alert({
    			    title: '支付结果',
    			    msg: '支付异常...',
     			   buttons: ['确定']
   			 });
   			 closeInfo1();
			}
		});
   	}
   	
   	function unionPay(){
// 	 var jsfun = 'show();';
//		api.execScript({
//		    script: jsfun
//		});
//		var card = "";
   	var dl_phone = $api.getStorage("dl_phone");
   	       var callback = function(ret, err){
             //console.log(JSON.stringify(ret));
             var jsfun = 'hide();';
				api.execScript({
				    script: jsfun
				});
                 if(ret){
              	   if(ret.msg == 1){
              	     cardNo = ret.cardno;
              	     cardJg = ret.qc;
              	     cardType = ret.ctype;
              	     isCard = 1;
              	     if(cardType != "" && cardType == "01"){
					     card = cardJg + "借记卡(" + cardNo + ")"; 
					  }else if(cardType != "" && cardType == "02"){
					     card = cardJg + "贷记卡(" + cardNo + ")"; 
					  }
					  $api.setStorage("card",card);
              	   }else if(ret.msg == 2){
              	     cardNo = ret.cardno;
              	     isCard = 2;
              	     card = cardNo;
              	     $api.setStorage("card",card);
              	   }
              	   
              	   if(isCard == 0){
				   	   if(api.systemType=='ios'){
					       api.confirm({
					          title: '温馨提示',
					          msg:'对不起，您还没绑定银行卡，请先绑定银行卡！',
					          buttons: ['去绑卡', '暂时不用']
					        }, function(ret, err) {
					          var index = ret.buttonIndex;
					          if(index==1){
					          api.openWin({
									name: 'card',
					                url:url.xzyhk+"?dlPhone="+dl_phone
					                //url:"http://192.168.85.32:8181/wx/unionPay/card_add.jsp?dlPhone="+dl_phone
					                });
					          }
					        });
					      }else{
					        var r=confirm("您还没有绑卡哦，确定现在去绑卡吗？");
					        if(r){
					           window.setTimeout(function(){
							       api.closeWin({});
							  },1000)
					          api.openWin({
									name: 'card',
					                url:url.xzyhk+"?dlPhone="+dl_phone
					                //url:"http://192.168.85.32:8181/wx/unionPay/card_add.jsp?dlPhone="+dl_phone
					          });
					        }
					      }
				   	 }else{
				   	   if(api.systemType == "android"){
						 $api.setStorage("frame","");
						 }
						var jsfun = 'uPay();';
						api.execScript({
						    script: jsfun
						});
						api.closeFrame({
					    });
					  }
              	 }
		    }
		    var content = {"dl_phone":dl_phone};
		    apiAjax(JSON.stringify(content),url.zcylzf,callback);
   	}
   	
  
</script>
</html>